package com.rabbitmq.config.delay;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王霄
 * @date 2022/1/8 20:51
 */
@Configuration
public class DelayConfig {

    public static final String DELAY_EXCHANGE_NAME = "delay.exchange";
    public static final String DELAY_QUEUE = "delay.queue";
    public static final String DELAY_ROUTING_KEY = "delay.routing.key";


    @Bean(DELAY_EXCHANGE_NAME)
    public CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    @Bean(DELAY_QUEUE)
    public Queue delayQueue() {
        return new Queue(DELAY_QUEUE);
    }

    @Bean
    public Binding bindingDelayExchange() {
        return BindingBuilder
                .bind(delayQueue())
                .to(customExchange())
                .with(DELAY_ROUTING_KEY).noargs();
    }

}
